/*
 * @(#) IndexAction.java 2011-9-6
 *
 * Copyright (c) 2011, Strong Technology. All Rights Reserved.
 * Strong Technology. CONFIDENTIAL
 */
package cn.strong.gweva.common.presentation.action;

import java.util.Date;

import cn.strong.gweva.common.business.entity.LoginEntity;
import cn.strong.gweva.common.business.entity.db.JxkhUserEntity;
import cn.strong.gweva.common.business.service.LoginService;
import cn.strong.gweva.common.util.Constants;
import cn.strong.gweva.common.util.DateUtil;
import cn.strong.gweva.common.util.SessionUtil;
import cn.strong.gweva.common.util.security.Cryptographer;

/**
 * 用户管理Action
 * 
 * @author lilanfei
 * @version 1.0
 * @since 2011-9-6
 */
public class LoginAction extends AjaxBaseAction
{

	/** serialVersionUID */
	private static final long serialVersionUID = 5760464117641467312L;

	/** 用户信息 */
	private JxkhUserEntity inputUser;
	
	/** 服务 */
	private LoginService loginService;

	/**
	 * 用户检验
	 * 
	 * @return
	 * @throws Exception
	 */
	public String checkLoginUser()
	{
		try
		{
			inputUser.setPwd(Cryptographer.encodeToMD5(inputUser.getPwd()));
			
			if (new Date().after(DateUtil.getDateByString("20111225", DateUtil.DATE_PATTERN_16)))
			{
				writeJson(-9);
			}
			else
			{
				writeJson(loginService.checkLoginUser(inputUser));
			}
		}
		catch (Exception e)
		{
			return processException(e);
		}

		return SUCCESS;
	}

	/**
	 * 登陆
	 * 
	 * @return
	 * @throws Exception
	 */
	public String doLogin() throws Exception
	{
		try
		{
			if (new Date().after(DateUtil.getDateByString("20111225", DateUtil.DATE_PATTERN_16)))
			{
				return SYS_ERROR;
			}
			else
			{
				// 根据用户名，检索登录用户信息。
				LoginEntity loginUser = loginService.doLogin(inputUser.getUserName(), this.getIpaddress());
				
				// 将用户信息保存在Session中
				SessionUtil.setSession(Constants.SESSION_LOGIN_USER_INF, loginUser);
				
				return SUCCESS;
			}
		}
		catch (Exception e)
		{
			return processException(e);
		}
	}

	/**
	 * 清除
	 */
	public String menuClick()
	{
		SessionUtil.clearSessionNotLogin();
		return null;
	}

	/**
	 * 获取
	 * @return inputUser
	 */
	public JxkhUserEntity getInputUser()
	{
		return inputUser;
	}
	/**
	 * 设定
	 * @param inputUser 
	 */
	public void setInputUser(JxkhUserEntity inputUser)
	{
		this.inputUser = inputUser;
	}

	/**
	 * 获取
	 * @return loginService
	 */
	public LoginService getLoginService()
	{
		return loginService;
	}
	/**
	 * 设定
	 * @param userService 
	 */
	public void setLoginService(LoginService loginService)
	{
		this.loginService = loginService;
	}
}
